package cn.roderick.web.httpservlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

public class MyHttpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        ServletContext sct=getServletConfig().getServletContext();

        // 从上下文环境中通过属性名获取属性值

        Map<Integer,String> dept=(Map<Integer,String>)sct.getAttribute("dept");

        Set<Integer> key=dept.keySet();

        response.setContentType("text/html;charset=utf-8");

        PrintWriter out=response.getWriter();

        out.println("<html>");

        out.println("<body>");

        out.println("<form action='/register' action='post'>");

        out.println("<table alignb='center'>");

        out.println("<tr>");

        out.println("<td>");

        out.println("username:");

        out.println("</td>");

        out.println("<td>");

        out.println("<input type='text' name='username'");

        out.println("</tr>");

        out.println("<tr>");

        out.println("<td>");

        out.println("city:");

        out.println("</td>");

        out.println("<td>");

        out.println("<select name='dept'");

        for(Integer i:key){

            out.println("<option value='"+i+"'>"+dept.get(i)+"</option>");

        }

        out.println("</select>");
        out.println("</td>");
        out.println("<tr>");
        out.println("</table>");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
        out.flush();

    }

}